export const loadCheckContent = async (
  url,
  renderCallback = () => {},
  sid = null,
  session_id = '',
  header = {}
) => {
  const items = []
  let buffer = ''
  let previousLine

  const userId = '1'

  try {
    const params = { front_session_id: session_id }

    let response = await fetch('http://134.84.22.224:10017/api/intelliTroubleshootingCheck', {
      method: 'post',
      headers: {
        ...header,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ front_session_id: session_id })
    })

    console.log(response)

    if (!response.ok) {
      renderCallback(
        {
          data: {
            content: 'error',
            isMarkdown: false
          }
        },
        sid
      )
      return items
    }

    const reader = response.body?.getReader()

    const decoder = new TextDecoder()
    let done = false

    while (!done) {
      const { value, done: readerDone } = await reader.read()
      done = readerDone
      // console.log("value", value);

      if (value) {
        buffer = decoder.decode(value, { stream: true })
        console.log(buffer)
        try {
          renderCallback(buffer, sid)
        } catch (e) {
          console.log(e)
        }
      }
    }
  } catch (error) {
    console.error('Error loading NDJSON data:', error)
    throw error
  }

  return items
}
